const MyTools = {
  //TODO 对象深度拷贝
  deepCopy: function (_object) {
    return JSON.parse(JSON.stringify(_object));
  },

  // TODO 金额格式化
  priceFormat: function (num, n) {
    num = parseFloat(num) || 0;
    n = n || 2;
    let symbol = ",";
    if (num === null) return num;
    if (typeof num !== "number")
      throw new TypeError("num参数应该是一个number类型");
    if (n < 0) throw new Error("参数n不应该小于0");
    let hasDot = parseInt(num) != num; //这里检测num是否为小数，true表示小数
    let m = n != undefined && n != null ? n : 1;
    num =
      m == 0
        ? num.toFixed(m) + "."
        : hasDot
        ? n
          ? num.toFixed(n)
          : num
        : num.toFixed(m);
    symbol = symbol || ",";
    num = num
      .toString()
      .replace(/(\d)(?=(\d{3})+\.)/g, function (match, p1, p2) {
        return p1 + symbol;
      });
    if (n == 0 || (!hasDot && !n)) {
      //如果n为0或者传入的num是整数并且没有指定整数的保留位数，则去掉前面操作中的小数位
      num = num.substring(0, num.indexOf("."));
    }
    return num;
  },
  // TODO 取消金额格式化
  delcommafy: function (num) {
    if (!num) return num;
    num = num.toString();
    num = num.replace(/,/gi, "");
    return parseInt(num);
  },
};

export default MyTools;
